﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml.Serialization;
using System.IO;

namespace CSharpIrcBot.UserControls
{
    public partial class ucConnectionOptions : UserControl
    {
        public Connection Connection
        {
            get { return (Connection) source[cboHostList.SelectedIndex]; }
        }

        public ucConnectionOptions()
        {
            InitializeComponent();
            if( !Utilities.IsDesignMode )
            {
                source.DataSource = ConnectionSettings.LoadConnections();
            }
        }

        private void cboHostList_SelectedIndexChanged( object sender, EventArgs e )
        {
            if( sender != null || ( sender is ComboBox ) )
            {
                ComboBox cbo = sender as ComboBox;

                if( cbo.SelectedIndex > -1 )
                {
                    source.CurrencyManager.Position = cbo.SelectedIndex;
                }
            }
        }

        public void SaveConnections()
        {
            ConnectionSettings.SaveConnections( source.DataSource );
        }

        private void btnAdd_Click( object sender, EventArgs e )
        {
            Connection newCon = new Connection();
            source.CurrencyManager.Position = source.Add( newCon );
        }

        private void btnAddChannel_Click( object sender, EventArgs e )
        {
            Dialog input = new Dialog( "Enter a channel name.", "Add a channel" );
            if( input.ShowDialog() == DialogResult.OK )
            {
                channelsBindingSource.Add( input.Result );
            }
        }

        private void btnRemoveChannel_Click( object sender, EventArgs e )
        {
            if( lstChannels.SelectedIndex > -1 )
            {
                channelsBindingSource.RemoveAt( lstChannels.SelectedIndex );
            }
        }
    }
}
